package com.xindacloud.sms.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.portal.dao.SmsUiContentMapper;
import com.xindacloud.sms.portal.dto.SmsUiContentDTO;
import com.xindacloud.sms.portal.entity.SmsUiContent;
import com.xindacloud.sms.portal.service.ISmsUiContentService;

/**
 * <p>
 * 系统主菜单表 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsUiContentServiceImpl extends ServiceImpl<SmsUiContentMapper, SmsUiContent> implements ISmsUiContentService {

	@Override
	public Object getSmsUiContentList(SmsUiContentDTO smsUiContentDTO) {
		IPage<SmsUiContent> page=new Page<>(smsUiContentDTO.getPageNum(),smsUiContentDTO.getPageSize());
		QueryWrapper<SmsUiContent> queryWrapper=new QueryWrapper<>();
		if(smsUiContentDTO.getParentId()!=null) {
			queryWrapper.eq("parent_id", smsUiContentDTO.getParentId());
		}
		return this.page(page,queryWrapper);
	}

	@Override
	public Object getSmsProductList(SmsUiContentDTO smsUiContentDTO) {
		SmsUiContentDTO ssdto=new SmsUiContentDTO();
		ssdto.setId(1);
		getSystemMenus(ssdto);
		return ssdto.getChildren();
	}
	
	private SmsUiContentDTO getSystemMenus(SmsUiContentDTO menuDto) {
		List<SmsUiContentDTO> dtoList=new ArrayList<>();
		QueryWrapper<SmsUiContent> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("parent_id", menuDto.getId());
		List<SmsUiContent> menusList=this.list(queryWrapper);
		if(menusList!=null&& menusList.size()>0) {
		for(int i=0;i<menusList.size();i++) {
			SmsUiContentDTO dto2=new SmsUiContentDTO();
			BeanUtils.copyProperties(menusList.get(i),dto2);
			if(menuDto.getId()==0) {
				dto2.setParentId(null);
			}
			dtoList.add(dto2);
		}
		menuDto.setChildren(dtoList);
		}
		if(menusList!=null && menusList.size()>0) {
		getMenuList(menuDto);
		}
		return menuDto;
	}

	private void getMenuList(SmsUiContentDTO menuDto) {
		for(int i=0;i<menuDto.getChildren().size();i++) {
			getSystemMenus(menuDto.getChildren().get(i));
		}
	}

}
